package com.xiaode.controller.app.myCampus.share;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.Activity;
import com.xiaode.common.model.ActivityUser;
import com.xiaode.common.model.Admin;
import com.xiaode.common.model.CampusShareComments;
import com.xiaode.common.model.CampusShareOriginal;
import com.xiaode.common.model.CampusShareTeacherRecommend;
import com.xiaode.common.model.ExampleActivityTask;
import com.xiaode.common.model.KeyResultsTask;
import com.xiaode.common.model.LabourTask;
import com.xiaode.common.model.LikeCampusShare;
import com.xiaode.common.model.LikeComments;
import com.xiaode.common.model.OrdinaryUserTask;
import com.xiaode.common.model.ShareLog;
import com.xiaode.common.model.Student;
import com.xiaode.common.model.VocationTask;
import com.xiaode.exce.EnumError;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ShareService {

	
	/**
	 * 查询教师推荐详情
	 */
	public Ret findRecommendDetails(Integer accountIdMy, Integer userTypeMy, Integer recommendId){
		CampusShareTeacherRecommend teacherRecommend = CampusShareTeacherRecommend.dao.findById(recommendId);
        //添加创建人信息
        Record createAccount = Redis.use().hget(Const.user.AccountIdInfo, teacherRecommend.getCreateAccountId());
        Integer createUserType = createAccount.getInt("userType");
        if (createUserType == Const.UserType.student) {
            Student o = Redis.use().get(Const.user.Student + createAccount.getInt("id"));
            teacherRecommend.put("createName", o.getName());
            teacherRecommend.put("createType", createUserType);
            teacherRecommend.put("pic", o.getAvatarUrl());
            teacherRecommend.put("sex", o.getSex());
        } 
        if (createUserType == Const.UserType.admin) {
            Admin o = Redis.use().get(Const.user.Admin + createAccount.getInt("id"));
            teacherRecommend.put("createName", o.getName());
            teacherRecommend.put("createType", createUserType);
            teacherRecommend.put("pic", o.getAvatarUrl());
            teacherRecommend.put("sex", o.getGender());
        }
        //添加推荐人信息
        Integer recommendAccountId = teacherRecommend.getRecommendAccountId();
        if (recommendAccountId != null) {
            Record recommendAccount = Redis.use().hget(Const.user.AccountIdInfo, recommendAccountId);
            Integer recommendUserType = recommendAccount.getInt("userType");
            Admin o = Redis.use().get(Const.user.Admin + recommendAccount.getInt("id"));
            teacherRecommend.put("recName", o.getName());
            teacherRecommend.put("recType", recommendUserType);
            teacherRecommend.put("recPic", o.getAvatarUrl());
            teacherRecommend.put("recSex", o.getGender());
        }
        //内容点赞信息
        List<LikeCampusShare> campusSharesRe = LikeCampusShare.dao.find("SELECT like_account_id FROM like_campus_share WHERE campus_share_id = ? AND type = ?", recommendId, Const.campusShareType.RECOMMEND);
        Set<Integer> reSet = campusSharesRe.stream().map(likeCampusShare -> likeCampusShare.getLikeAccountId()).collect(Collectors.toSet());
        //内容评论数量
        Integer reComNum = Db.queryInt("SELECT COUNT(*) FROM campus_share_comments WHERE campusShareId = ? AND type = ?", recommendId, Const.campusShareType.RECOMMEND);
        //对应的被推荐的内容信息
        Integer taskId = teacherRecommend.getTaskId();
        switch (teacherRecommend.getTaskType()) {
            case Const.taskType.ORDINARY_USER_TASK_TYPE:
                teacherRecommend.put("task", OrdinaryUserTask.dao.findById(taskId));
                break;
            case Const.taskType.LABORU_TASK_TYPE:
                teacherRecommend.put("task", LabourTask.dao.findById(taskId));
                break;
            case Const.taskType.VOCATION_TASK_TYPE:
                teacherRecommend.put("task", VocationTask.dao.findById(taskId));
                List<KeyResultsTask> keyResultsTasks = KeyResultsTask.dao.find("SELECT * FROM key_results_task WHERE task_id = ? AND task_type = ? ", taskId, Const.taskType.VOCATION_TASK_TYPE);
                teacherRecommend.put("keyResults", keyResultsTasks);
                break;
            case Const.taskType.EXAMPLE_ACTIVITY_TASK_TYPE:
                teacherRecommend.put("task", ExampleActivityTask.dao.findById(taskId));
                List<KeyResultsTask> keyResultsTaskList = KeyResultsTask.dao.find("SELECT * FROM key_results_task WHERE task_id = ? AND task_type = ? ", taskId, Const.taskType.EXAMPLE_ACTIVITY_TASK_TYPE);
                teacherRecommend.put("keyResults", keyResultsTaskList);
                break;
            case Const.taskType.ACTIVITY:
            	ActivityUser activityUser = ActivityUser.dao.findById(taskId);
                teacherRecommend.put("task", Activity.dao.findById(activityUser.getActivityId()));
                teacherRecommend.put("activityUser", activityUser);
                break;
            default:
                return Ret.fail().set("error", EnumError.TASK_TYPE_ERROR.getObj());
        }
        return Ret.ok().set("campus", teacherRecommend).set("likeNum", reSet.size()).set("isLike", reSet.contains(accountIdMy)).set("commentNum", reComNum);
	}
	
	/**
     * 查询我的校园详情页
     */
    public Ret findOriginalDetails(Integer accountIdMy, Integer userTypeMy, Integer originalId) {
        CampusShareOriginal shareOriginal = CampusShareOriginal.dao.findById(originalId);
        //添加创建人信息
        Record createAccount = Redis.use().hget(Const.user.AccountIdInfo, shareOriginal.getUserAccountId());
        Integer createUserType = createAccount.getInt("userType");
        if (createUserType == Const.UserType.student) {
            Student o = Redis.use().get(Const.user.Student + createAccount.getInt("id"));
            if (BeanUtil.isEmpty(o)) {
                return Ret.fail().set("error", EnumError.USER_NULL.getObj());
            }
            shareOriginal.put("createName", o.getName());
            shareOriginal.put("createType", createUserType);
            shareOriginal.put("pic", o.getAvatarUrl());
            shareOriginal.put("sex", o.getSex());
        }
        if (createUserType == Const.UserType.admin) {
            Admin o = Redis.use().get(Const.user.Admin + createAccount.getInt("id"));
            if (BeanUtil.isEmpty(o)) {
                return Ret.fail().set("error", EnumError.USER_NULL.getObj());
            }
            shareOriginal.put("createName", o.getName());
            shareOriginal.put("createType", createUserType);
            shareOriginal.put("pic", o.getAvatarUrl());
            shareOriginal.put("sex", o.getGender());
        }
        //内容点赞信息
        List<LikeCampusShare> campusSharesOr = LikeCampusShare.dao.find("SELECT like_account_id FROM like_campus_share WHERE campus_share_id = ? AND type = ?", originalId, Const.campusShareType.ORIGINAL);
        Set<Integer> orSet = campusSharesOr.stream().map(likeCampusShare -> likeCampusShare.getLikeAccountId()).collect(Collectors.toSet());
        //内容评论数量
        Integer orComNum = Db.queryInt("SELECT COUNT(*) FROM campus_share_comments WHERE campusShareId = ? AND type = ?", originalId, Const.campusShareType.ORIGINAL);
        
        return Ret.ok().set("campus", shareOriginal).set("likeNum", orSet.size()).set("isLike", orSet.contains(accountIdMy)).set("commentNum", orComNum);

        
    }
	
	
	
	
	
	
	
	/**
     * 查询原创发布列表
     */
    public Ret findCampusOriginalByPage(Integer accountIdMy, Integer createAccountId, Integer page, Integer limit) throws Exception {
        Cache redis = Redis.use();
        //查询校园分享
        Page<CampusShareOriginal> campusOriginalPage = CampusShareOriginal.dao.template("findCampusOriginalList", Kv.of("userAccountId", createAccountId)).paginate(page, limit);
        for (CampusShareOriginal campusShareOriginal : campusOriginalPage.getList()) {
        	//如果原创发布来自活动的心得分享
            Integer activityId = campusShareOriginal.getActivityId();
            if (activityId != null) {
                Activity activity = Activity.dao.findById(activityId);
                campusShareOriginal.put("activity", activity);
            }
            //添加发布人信息
            Record createUserInfo = redis.hget(Const.user.AccountIdInfo, campusShareOriginal.getUserAccountId());
            Integer creType = createUserInfo.getInt("userType");
            if (creType == Const.UserType.student) {
                Student student = redis.get(Const.user.Student + createUserInfo.getInt("id"));
                campusShareOriginal.put("createName", student.getName());
                campusShareOriginal.put("createPic", student.getAvatarUrl());
                campusShareOriginal.put("sex", student.getSex());
            } else {
                Admin admin = redis.get(Const.user.Admin + createUserInfo.getInt("id"));
                campusShareOriginal.put("createName", admin.getName());
                campusShareOriginal.put("createPic", admin.getAvatarUrl());
                campusShareOriginal.put("sex", admin.getGender());
            }
            //查询点赞数与回复数
            Integer shareOriginalId = campusShareOriginal.getId();
            Integer likeNum = Db.queryInt("SELECT COUNT(*) FROM like_campus_share WHERE campus_share_id = ? AND type = ?", shareOriginalId, Const.campusShareType.ORIGINAL);
            Integer commentNum = Db.queryInt("SELECT COUNT(*) FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareOriginalId, Const.campusShareType.ORIGINAL);
            campusShareOriginal.put("likeNum", likeNum);
            campusShareOriginal.put("commentNum", commentNum);
            //查询当前用户是否点赞过该分享
            LikeCampusShare likeCampusShare = LikeCampusShare.dao.findFirst("SELECT id FROM like_campus_share WHERE campus_share_id = ? AND like_account_id = ? AND type = ?", shareOriginalId, accountIdMy, Const.campusShareType.ORIGINAL);
            if (BeanUtil.isEmpty(likeCampusShare)) {
            	campusShareOriginal.put("selfLike", false);
            } else {
            	campusShareOriginal.put("selfLike", true);
            }
        }
        return Ret.ok("campusOriginalPage", campusOriginalPage);
    }

    /**
     * 创建原创发布
     */
    @Before(Tx.class)
    public Ret creatCampusOriginal(Integer accountId, Integer userType, String title, String content, String files) throws Exception {
    	
        CampusShareOriginal campusShare = new CampusShareOriginal();
        campusShare.setTitle(title);
        campusShare.setContent(content);
        campusShare.setUserAccountId(accountId);
        campusShare.setFiles(files);
        campusShare.save();
        
        return Ret.ok();
    }

    /**
     * 删除原创发布
     */
    @Before(Tx.class)
    public Ret delCampusOriginal(Integer accountId, Integer shareId) throws Exception {

        CampusShareOriginal campusShare = CampusShareOriginal.dao.findFirst("SELECT id FROM campus_share_original where user_account_id = ? AND id = ?", accountId, shareId);
        //查询为空
        if (BeanUtil.isEmpty(campusShare)) {
            return Ret.fail().set("error", EnumError.SHARE_NOT_CREATE_BY_YOU.getObj());
        }
        campusShare.delete();

        //删除评论 查询该分享下的所有评论
        List<CampusShareComments> campusShareCommentes = CampusShareComments.dao.find("SELECT * FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareId, Const.campusShareType.ORIGINAL);
        //所有评论的id
        List<Integer> collect = campusShareCommentes.stream().map(campusShareCommentes1 -> campusShareCommentes1.getId()).collect(Collectors.toList());
        //删除所有点赞评论
        if (ObjectUtil.isAllNotEmpty(collect)) {
            Kv kv = Kv.of("likeCommentsList", collect);
            Db.template("delLikeComments", kv).delete();
        }
        // 删除点赞
        Db.delete("DELETE FROM like_campus_share WHERE campus_share_id = ? AND type = ?", shareId, Const.campusShareType.ORIGINAL);
        //删除评论
        Db.delete("DELETE FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareId, Const.campusShareType.ORIGINAL);
        
        return Ret.ok();
    }
    
    /**
     * 查询原创发布详情
     */
    public Ret findCampusOriginalInfo(Integer accountId, Integer campusId) throws Exception {
        CampusShareOriginal original = CampusShareOriginal.dao.findById(campusId);
        if (BeanUtil.isEmpty(original)) {
            return Ret.fail().set("error", EnumError.TASK_NOT_EXIST.getObj());
        }
        Integer activityId = original.getActivityId();
        if (activityId == null) {
            return Ret.fail().set("error", EnumError.CAMPUS_TYPE_NOT_CORRECT.getObj());
        }
        Activity activity = Activity.dao.findById(activityId);//可以为空

        Integer userAccountId = original.getUserAccountId();
        ActivityUser activityUser = ActivityUser.dao.findFirst("SELECT * FROM activity_user WHERE activity_id = ? AND student_account_id = ?", activityId, userAccountId);
        if (BeanUtil.isEmpty(activityUser)) {
            return Ret.fail().set("error", EnumError.ACTIVITY_USER_NOT_EXIST.getObj());
        }
        //查询点赞数量
        List<LikeCampusShare> likeCampusShares = LikeCampusShare.dao.find("SELECT like_account_id FROM like_campus_share WHERE campus_share_id = ? AND type = ?", campusId, Const.campusShareType.ORIGINAL);
        original.put("likeNum",likeCampusShares.size()).put("isLike",likeCampusShares.stream().map(LikeCampusShare::getLikeAccountId).collect(Collectors.toSet()).contains(accountId));
        
        return Ret.ok("campus", original).set("activity", activity).set("activityUser", activityUser);
    }
    
    
    
    
    
    
    /**
     * 查询教师推荐
     */
    public Ret findCampusTeacherRecommendByPage(Integer accountIdMy, Integer createAccountId, Boolean isMy, Integer recommendAccountId, Integer page, Integer limit) throws Exception {
        Cache redis = Redis.use();
        //查询校园分享
        Page<CampusShareTeacherRecommend> campusTeacherRecommendPage = CampusShareTeacherRecommend.dao.template("campusTeacherRecommend", Kv.of("accountIdMy", accountIdMy).set("isMy", isMy).set("recommendAccountId", recommendAccountId).setIfNotNull("createAccountId", createAccountId)).paginate(page, limit);
        for (CampusShareTeacherRecommend recommend : campusTeacherRecommendPage.getList()) {
        	//添加发布人信息
            Record createUserInfo = redis.hget(Const.user.AccountIdInfo, recommend.getCreateAccountId());
            Student student = redis.get(Const.user.Student + createUserInfo.getInt("id"));
            recommend.put("createName", student.getName());
            recommend.put("createPic", student.getAvatarUrl());
            recommend.put("sex", student.getSex());
            //查询点赞数与回复数
            Integer recommendId = recommend.getId();
            Integer likeNum = Db.queryInt("SELECT COUNT(*) FROM like_campus_share WHERE campus_share_id = ? AND type = ?", recommendId, Const.campusShareType.RECOMMEND);
            Integer commentNum = Db.queryInt("SELECT COUNT(*) FROM campus_share_comments WHERE campusShareId = ? AND type = ?", recommendId, Const.campusShareType.RECOMMEND);
            recommend.put("likeNum", likeNum);
            recommend.put("commentNum", commentNum);
            //查询当前用户是否点赞过该分享
            LikeCampusShare likeCampusShare = LikeCampusShare.dao.findFirst("SELECT id FROM like_campus_share WHERE campus_share_id = ? AND like_account_id = ? AND type = ?", recommendId, accountIdMy, Const.campusShareType.RECOMMEND);
            if (BeanUtil.isEmpty(likeCampusShare)) {
            	recommend.put("selfLike", false);
            } else {
            	recommend.put("selfLike", true);
            }
        }
        return Ret.ok("campusTeacherRecommendPage", campusTeacherRecommendPage);
    }

    /**
     * 创建教师推荐
     */
    public Ret creatCampusTeacherRecommend(Integer accountId, String title, String content, String files, Integer userAccountId, Integer taskType, Integer taskId) throws Exception {  	
    	
		CampusShareTeacherRecommend campusShare = CampusShareTeacherRecommend.dao.findFirst("SELECT * FROM campus_share_teacher_recommend WHERE task_type = ? AND task_id = ?", taskType, taskId);
		if(BeanUtil.isEmpty(campusShare)) {
    		campusShare = new CampusShareTeacherRecommend();
	        campusShare.setTitle(title);
	        campusShare.setContent(content);
	        campusShare.setCreateAccountId(userAccountId);
	        campusShare.setRecommendAccountId(accountId);
	        campusShare.setFiles(files);
	        campusShare.setTaskType(taskType);
	        campusShare.setTaskId(taskId);
	        campusShare.save();
		}
		
        return Ret.ok();
    }

    /**
     * 删除教师推荐
     */
    @Before(Tx.class)
    public Ret delCampusTeacherRecommend(Integer accountId, Integer shareId) throws Exception {
        CampusShareTeacherRecommend campusShare = CampusShareTeacherRecommend.dao.findFirst("SELECT id FROM campus_share_teacher_recommend where recommend_account_id = ? AND id = ?", accountId, shareId);
        //查询为空
        if (BeanUtil.isEmpty(campusShare)) {
            return Ret.fail().set("error", EnumError.SHARE_NOT_CREATE_BY_YOU.getObj());
        }
        campusShare.delete();

        //删除评论 查询该分享下的所有评论
        List<CampusShareComments> campusShareCommentes = CampusShareComments.dao.find("SELECT * FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareId, Const.campusShareType.RECOMMEND);
        //所有评论的id
        List<Integer> collect = campusShareCommentes.stream().map(campusShareCommentes1 -> campusShareCommentes1.getId()).collect(Collectors.toList());
        //删除所有点赞评论
        if (ObjectUtil.isAllNotEmpty(collect)) {
            Kv kv = Kv.of("likeCommentsList", collect);
            Db.template("delLikeComments", kv).delete();
        }
        // 删除点赞
        Db.delete("DELETE FROM like_campus_share WHERE campus_share_id = ? AND type = ?", shareId, Const.campusShareType.RECOMMEND);
        //删除评论
        Db.delete("DELETE FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareId, Const.campusShareType.RECOMMEND);
        
        return Ret.ok();
    }
    
    
    
    
    
    
    
    
    /**
     * 查询第一层评论
     */
    public Ret findComments( Integer shareId, Integer page, Integer limit, Integer shareType) throws Exception {
        Cache redis = Redis.use();
        Page<CampusShareComments> commentesPage = CampusShareComments.dao.paginate(page, limit, "SELECT * ", "FROM campus_share_comments WHERE campusShareId = ? AND stratum = ? AND type = ? ORDER BY id DESC", shareId, Const.campusShareCommentStratum.SHARE, shareType);
        List<CampusShareComments> list = commentesPage.getList();
        for (CampusShareComments campusShareCommentes : list) {
            Integer num = Db.queryInt("SELECT count(*) FROM campus_share_comments WHERE topComments = ?", campusShareCommentes.getId());
            campusShareCommentes.put("commentsNum", num);
            //名称 头像
            Record record = redis.hget(Const.user.AccountIdInfo, campusShareCommentes.getCreateAccountId());
            Integer createType = record.getInt("userType");
            if (createType == Const.UserType.student) {
                Student student = redis.get(Const.user.Student + record.getInt("id"));
                campusShareCommentes.put("createName", student.getName());
                campusShareCommentes.put("createPic", student.getAvatarUrl());
                campusShareCommentes.put("sex", student.getSex());
            } 
            if (createType == Const.UserType.admin) {
                Admin admin = redis.get(Const.user.Admin + record.getInt("id"));
                campusShareCommentes.put("createName", admin.getName());
                campusShareCommentes.put("createPic", admin.getAvatarUrl());
            }
        }
        //查询数量
        Integer commentsNum = Db.queryInt("SELECT COUNT(*) FROM campus_share_comments WHERE campusShareId = ? AND type = ?", shareId, shareType);
        return Ret.ok("page", commentesPage).set("commentNum", commentsNum);
    }

    /**
     * 查询第二层评论
     */
    public Ret findSecondComments(Integer shareId, Integer topCommentsId) throws Exception {
        Cache redis = Redis.use();
        List<CampusShareComments> list = CampusShareComments.dao.find("SELECT id,comments,commentsId,create_account_id,`addTime` FROM campus_share_comments WHERE campusShareId = ? AND topComments = ? ORDER BY id DESC", shareId, topCommentsId);
        list.stream().forEach(campusShareComments -> {
            Record record = redis.hget(Const.user.AccountIdInfo, campusShareComments.getCreateAccountId());
            Integer userType = record.getInt("userType");
            if (userType == Const.UserType.student) {
                Student student = redis.get(Const.user.Student + record.getInt("id"));
                campusShareComments.put("createName", student.getName());
                campusShareComments.put("createPic", student.getAvatarUrl());
                campusShareComments.put("sex", student.getSex());
            } 
            if (userType == Const.UserType.admin) {
                Admin admin = redis.get(Const.user.Admin + record.getInt("id"));
                campusShareComments.put("createName", admin.getName());
                campusShareComments.put("createPic", admin.getAvatarUrl());
            }
        });
        Map<Integer, CampusShareComments> map = list.stream().collect(Collectors.toMap(CampusShareComments::getId, campusShareComments -> campusShareComments));
        list.stream().forEach(campusShareComments -> {
            if (!campusShareComments.getCommentsId().equals(topCommentsId)) {
                String createName = map.get(campusShareComments.getCommentsId()).getStr("createName");
                campusShareComments.put("replayName", createName);
            }
        });
        return Ret.ok("comments", list);
    }

    /**
     * 对分享点赞/取消点赞
     */
    public Ret likeCampusShare(Integer accountId, Integer shareId, Integer shareType) throws Exception {
        if (shareType == Const.campusShareType.RECOMMEND) {
        	CampusShareTeacherRecommend campusShareTeacherRecommend = CampusShareTeacherRecommend.dao.findById(shareId);
        	if(BeanUtil.isEmpty(campusShareTeacherRecommend)) {
        		return Ret.fail().set("error", EnumError.SHARE_HAS_DELETE.getObj());
        	}
        }
        if (shareType == Const.campusShareType.ORIGINAL) {
        	CampusShareOriginal campusShareOriginal = CampusShareOriginal.dao.findById(shareId);
        	if(BeanUtil.isEmpty(campusShareOriginal)) {
         		return Ret.fail().set("error", EnumError.SHARE_HAS_DELETE.getObj());
         	}
        }
        LikeCampusShare first = LikeCampusShare.dao.findFirst("SELECT id FROM like_campus_share WHERE campus_share_id = ? AND like_account_id = ? AND type = ?", shareId, accountId, shareType);
        if (BeanUtil.isEmpty(first)) {
            LikeCampusShare likeCampusShare = new LikeCampusShare();
            likeCampusShare.setCampusShareId(shareId);
            likeCampusShare.setLikeAccountId(accountId);
            likeCampusShare.setType(shareType);
            likeCampusShare.save();
        }else {
        	first.delete();
        }
        return Ret.ok();
    }

    /**
     * 点赞评论
     */
    public Ret likeComments(Integer accountId, Integer commentsId) throws Exception {

        CampusShareComments shareCommentes = CampusShareComments.dao.findById(commentsId);
        if (BeanUtil.isEmpty(shareCommentes)) {
            return Ret.fail().set("error", EnumError.COMMENTS_HAS_DELETE.getObj());
        }
        LikeComments first = LikeComments.dao.findFirst("SELECT id FROM like_comments WHERE like_account_id = ? AND comments_id = ?", accountId, commentsId);
        if (BeanUtil.isEmpty(first)) {
            LikeComments likeComments = new LikeComments();
            likeComments.setCommentsId(commentsId);
            likeComments.setLikeAccountId(accountId);
            likeComments.save();
        }else {
        	first.delete();
        }
        return Ret.ok();
    }

    /**
     * 发表评论
     */
    public Ret addComments(Integer accountId, Integer shareId, Integer commentsId, String comment, Integer topCommentsId, Integer shareType) throws Exception {
        
        if (shareType == Const.campusShareType.RECOMMEND) {
        	CampusShareTeacherRecommend campusShareTeacherRecommend = CampusShareTeacherRecommend.dao.findById(shareId);
        	if(BeanUtil.isEmpty(campusShareTeacherRecommend)) {
        		return Ret.fail().set("error", EnumError.SHARE_HAS_DELETE.getObj());
        	}
        }
        if (shareType == Const.campusShareType.ORIGINAL) {
        	CampusShareOriginal campusShareOriginal = CampusShareOriginal.dao.findById(shareId);
        	if(BeanUtil.isEmpty(campusShareOriginal)) {
         		return Ret.fail().set("error", EnumError.SHARE_HAS_DELETE.getObj());
         	}
        }
        if (BeanUtil.isNotEmpty(commentsId)) {
            CampusShareComments commentes = CampusShareComments.dao.findById(commentsId);
            if (BeanUtil.isEmpty(commentes)) {
                return Ret.fail().set("error", EnumError.COMMENTS_HAS_DELETE.getObj());
            }
        }
        if (StrUtil.isEmpty(comment)) {
            return Ret.fail().set("error", EnumError.COMMENT_NOT_ALLOW_EMPTY.getObj());
        }
        CampusShareComments shareCommentes = new CampusShareComments();
        shareCommentes.setCreateAccountId(accountId);
        shareCommentes.setComments(comment);
        shareCommentes.setType(shareType);
        shareCommentes.setCampusShareId(shareId);
        shareCommentes.setCommentsId(commentsId);
        if (commentsId == null) {
            shareCommentes.setStratum(Const.campusShareCommentStratum.SHARE);
        } else {
            shareCommentes.setStratum(Const.campusShareCommentStratum.REPLY);
        }
        shareCommentes.setTopComments(topCommentsId);
        shareCommentes.save();
        
        return Ret.ok();
    }

    /**
     * 删除评论
     */
    @Before(Tx.class)
    public Ret delComments(Integer accountId, Integer commentsId) throws Exception {
        CampusShareComments shareCommentes = CampusShareComments.dao.findById(commentsId);
        if (BeanUtil.isEmpty(shareCommentes)) {
            return Ret.fail().set("error", EnumError.COMMENTS_HAS_NOT_EXIT.getObj());
        }
        if (!shareCommentes.getCreateAccountId().equals(accountId)) {
        	return Ret.fail().set("error", EnumError.CMMENTS_NOT_BELONE_TO_YOU.getObj());
        }
        shareCommentes.delete();
        //查询子评论
        List<CampusShareComments> shareCommentesList = CampusShareComments.dao.find("SELECT id FROM campus_share_comments WHERE commentsId = ? OR topComments = ? ", commentsId, commentsId);
        if (ObjectUtil.isAllNotEmpty(shareCommentesList)) {
            List<Integer> collect = shareCommentesList.stream().map(campusShareCommentes -> campusShareCommentes.getId()).collect(Collectors.toList());
            Kv kv = Kv.of("likeCommentsList", collect);
            //删除点赞子评论
            Db.template("delLikeComments", kv).delete();
            //删除子评论
            Db.template("delComments", kv).delete();
        }
        //删除点赞与子评论
        Db.delete("DELETE FROM like_comments WHERE comments_id = ?", commentsId);
        
        return Ret.ok();
    }
    
    

    
    /**
     * 内容分享记录
     */
    @Before(Tx.class)
    public Ret shareContent(Integer accountId, Integer id, Integer shareType) throws Exception {
        if (shareType == Const.campusShareType.RECOMMEND) {
        	Db.update("UPDATE campus_share_teacher_recommend set forward_num = forward_num + 1 WHERE id = ?", id);
        }
        if (shareType == Const.campusShareType.ORIGINAL) {
        	Db.update("UPDATE campus_share_original set forward_num = forward_num + 1 WHERE id = ?", id);
        }
        ShareLog shareLog = new ShareLog();
        shareLog.setUserAccountId(accountId);
        shareLog.setCampusId(id);
        shareLog.setCampusType(shareType);
        shareLog.save();
        
        return Ret.ok();
    }
    
    
}
